package com.michaelbulava.TFS.ui.controls.workspaces;

import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.WorkingFolder;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import com.microsoft.tfs.util.Check;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Created by mbulava on 7/15/2015.
 */
public class WorkingFolderDataCollection
{
    private final List workingFolders = new ArrayList();

    public WorkingFolderDataCollection() {}

    public WorkingFolderDataCollection(Workspace workspace)
    {
        this(workspace.getFolders());
    }

    public WorkingFolderDataCollection(WorkingFolder[] workingFolders)
    {
        Check.notNull(workingFolders, "workingFolders");
        for (int i = 0; i < workingFolders.length; i++) {
            add(new WorkingFolderData(workingFolders[i]));
        }
    }

    public WorkingFolder[] createWorkingFolders()
    {
        WorkingFolder[] result = new WorkingFolder[this.workingFolders.size()];
        int ix = 0;
        for (Iterator it = this.workingFolders.iterator(); it.hasNext();)
        {
            WorkingFolderData workingFolderData = (WorkingFolderData)it.next();
            result[(ix++)] = workingFolderData.createWorkingFolder();
        }
        return result;
    }

    public WorkingFolderDataCollection(WorkingFolderData[] workingFolderData)
    {
        Check.notNull(workingFolderData, "workingFolderData");

        this.workingFolders.addAll(Arrays.asList(workingFolderData));
    }

    public WorkingFolderData[] getWorkingFolderData()
    {
        return (WorkingFolderData[])this.workingFolders.toArray(new WorkingFolderData[this.workingFolders.size()]);
    }

    public void add(WorkingFolderData workingFolderData)
    {
        Check.notNull(workingFolderData, "workingFolderData");

        this.workingFolders.add(workingFolderData);
    }

    public void remove(WorkingFolderData workingFolderData)
    {
        Check.notNull(workingFolderData, "workingFolderData");

        this.workingFolders.remove(workingFolderData);
    }
}
